package com.tuanzi.loan.business.service.settlement.impl;

import com.tuanzi.loan.business.constants.settlement.CompanyCooperationMode;
import com.tuanzi.loan.business.entity.settlement.CompanyInfo;
import com.tuanzi.loan.business.entity.settlement.SettlementPriceChangeRecord;
import com.tuanzi.loan.business.repository.settlement.SettlementPriceChangeRecordRepository;
import com.tuanzi.loan.business.service.settlement.CompanyInfoService;
import com.tuanzi.loan.business.service.settlement.SettlementPriceChangeRecordService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.repository.JPAAuditRepository;
import com.tuanzi.loan.core.service.BaseAuditServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.settlement.SettlementPriceChangeRecordRequest;
import com.tuanzi.loan.vo.response.settlement.SettlementPriceChangeRecordResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author caiwanlin
 * @create 2018-01-16 16:22
 */
@Debug
@Service
@Transactional
@Retryable(value = {RetryException.class})
public class SettlementPriceChangeRecordServiceImpl extends BaseAuditServiceImpl<SettlementPriceChangeRecord, Long> implements SettlementPriceChangeRecordService {
    @Autowired
    private SettlementPriceChangeRecordRepository settlementPriceChangeRecordRepository;

    @Autowired
    private CompanyInfoService companyInfoService;

    @Override
    protected JPAAuditRepository<SettlementPriceChangeRecord, Long> getRepository() {
        return settlementPriceChangeRecordRepository;
    }

    @Override
    public Page<SettlementPriceChangeRecordResponse> list(PageSearcher<SettlementPriceChangeRecordRequest> pageSearcher) {
        return settlementPriceChangeRecordRepository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
    }

    @Override
    public void findCompanyInfo(SettlementPriceChangeRecord record) {
//        String companyName1 = record.getCompanyName();
        Long companyId1 = record.getCompanyId();
//        String productName1 = record.getProductName();
        CompanyInfo companyInfo = companyInfoService.findOne(companyId1);
        if (companyInfo != null) {
            Long companyId = companyInfo.getId();
            String companyName = companyInfo.getCompanyName();
            String productName = companyInfo.getProductName();
            Integer copMode = companyInfo.getCopMode();
            Float settlementPriceCpa = companyInfo.getSettlementPriceCpa();
            Float settlementPriceCps = companyInfo.getSettlementPriceCps();
            record.setCompanyId(companyId);
            record.setCompanyName(companyName);
            record.setProductName(productName);
            record.setCopMode(copMode);
            record.setOldPercentPrice(settlementPriceCps);
            record.setOldYuanPrice(settlementPriceCpa);
        } else {
            record.setOldYuanPrice(null);
            record.setOldPercentPrice(null);
            record.setCompanyId(null);
            record.setCopMode(null);

        }

    }

    @Override
    public SettlementPriceChangeRecord saveChangeRecord(SettlementPriceChangeRecord record) {
        //插入变更记录表
        if (record.getEffectiveTime().compareTo(new Date()) < 0) {
            CompanyInfo companyInfo = companyInfoService.findOne(record.getCompanyId());
            //新的合作模式
            Integer newCopMode = record.getNewCopMode();
            //旧的合作模式
            Integer copMode = record.getCopMode();
            if (newCopMode != null && newCopMode != copMode){
                copMode = newCopMode;
            }
            if (copMode == CompanyCooperationMode.CPA.getIndex()) {
                companyInfo.setSettlementPriceCpa(record.getNewYuanPrice() == null ?
                        record.getOldYuanPrice() : record.getNewYuanPrice());
                companyInfo.setSettlementPriceCps(null);
            } else if (copMode == CompanyCooperationMode.CPS.getIndex()) {
                companyInfo.setSettlementPriceCps(record.getNewPercentPrice() == null ?
                        record.getOldPercentPrice() : record.getNewPercentPrice());
                companyInfo.setSettlementPriceCpa(null);
            } else if (copMode == CompanyCooperationMode.CPC.getIndex()) {
                companyInfo.setSettlementPriceCpa(record.getNewYuanPrice() == null ?
                        record.getOldYuanPrice() : record.getNewYuanPrice());
                companyInfo.setSettlementPriceCps(null);
            } else if (copMode == CompanyCooperationMode.CPA_CPS.getIndex()) {
                companyInfo.setSettlementPriceCpa(record.getNewYuanPrice() == null ?
                        record.getOldYuanPrice() : record.getNewYuanPrice());
                companyInfo.setSettlementPriceCps(record.getNewPercentPrice() == null ?
                        record.getOldPercentPrice() : record.getNewPercentPrice());
            }
            //更改公司的合作模式
            companyInfo.setCopMode(copMode);
            companyInfoService.saveOrUpdate(companyInfo);
            record = saveOrUpdate(record);
        } else {
            record = saveOrUpdate(record);
        }
        return record;
    }

}
